package com.jzg.mvc.api.barcode;

import java.util.Date;

import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.StoreBarcode;
import com.jzg.common.model.sys.SysOrg;
import com.jzg.common.util.UuidUtil;

@Path(value = "/api/barcode", viewPath = "")
public class StoreBarcodeController extends BaseController {

	protected final Log LOG = Log.getLog(getClass());

	public void save() {
		String caseNo = getPara("caseNo");
		//String policeNo = getPara("policeNo");
		if(StrKit.isBlank(caseNo)) {
			renderError("案件编号有误！");
			return;
		}
		StoreBarcode storeBarcode = new StoreBarcode();
		storeBarcode.setId(UuidUtil.getUUID());
		storeBarcode.setName(caseNo);
		storeBarcode.setCreateTime(new Date(System.currentTimeMillis()));
		if(storeBarcode.save()) {
			renderSuccess("成功！");
		}else {
			renderError("失败！");
		}
	}
	
	public void logListData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		String sqlExceptSelect = " FROM " + StoreBarcode.TABLE_NAME + " O,"+SysOrg.TABLE_NAME+" R  WHERE O.ORGID=R.ID ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keyword + "%' OR R.NAME LIKE '%" + keyword + "%' OR O.CREATEUSERNAME LIKE '%" + keyword + "%' ) ";
		}
		if(!StrKit.isBlank(startTime)) {
			sqlExceptSelect += " AND DATE_FORMAT(O.CREATETIME,'%Y-%m-%d %H:%i:%s') >= STR_TO_DATE('"+startTime+"','%Y-%m-%d %H:%i:%s') ";
		}
		if(!StrKit.isBlank(endTime)) {
			sqlExceptSelect += " AND DATE_FORMAT(O.CREATETIME,'%Y-%m-%d %H:%i:%s') <= STR_TO_DATE('"+endTime+"','%Y-%m-%d %H:%i:%s') ";
		}
		sqlExceptSelect += "  ORDER BY  O.CREATETIME DESC ";
		Page<StoreBarcode> page = StoreBarcode.dao.paginate(pageNumber, pageSize, "select o.*,r.`NAME` as orgName ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
}
